package com.Gwo.org.controller;

import com.Gwo.basic.utils.PageList;
import com.Gwo.basic.utils.Result;
import com.Gwo.org.domain.Employee;
import com.Gwo.org.query.EmployeeQuery;
import com.Gwo.org.service.IDeptService;
import com.Gwo.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的功能")
@RequestMapping("/emp")
@RestController
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findById(@PathVariable("id") Long id){
        return service.findById(id);
    }

    @GetMapping
    public List<Employee> findAll(){
        List<Employee> list = service.findAll();
//        list.forEach(System.out::println);
        return list;
    }

    @PutMapping
    public Result addOrUpdate(@RequestBody Employee employee){
        if(employee.getId()==null){
            try {
                service.add(employee);
                return Result.me().success();
            }catch (Exception e){
                e.printStackTrace();
            }
            return Result.me().fail();

        }else {
            try {
                service.update(employee);
                return Result.me().success();
            }catch (Exception e){
                e.printStackTrace();
            }
            return Result.me().fail();
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        try{
            service.delete(id);
            return Result.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        PageList<Employee> list = service.queryByPage(query);
//        list.getRows().forEach(System.out::println);
        return list;
    }

    @PatchMapping
    public Result batchDelete(@RequestBody List<Long> ids){
        try{
            service.batchDelete(ids);
            return Result.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.me().fail();
    }
}
